/**
 * Created by lixiaodi on 2019/06/17 0027.
 */
mini.parse();
//得到数据表
var grid = mini.get('datagrid1');
var userInfo = {};
var userNumber1;

//带有父页面传来的值
function SetData(data){
	console.log(data);
	userInfo = data.data;
	userNumber1=data.data.userNumber;
	console.log(userNumber1);
	//页面初始化
	$(function (){
		httpRequest();
	});
}


//保存
function SaveData(){
	var selectUsers = grid.getSelecteds();
	if(selectUsers){
		var userArr = [];
		for(var i = 0; i < selectUsers.length; i++){
			var usersObj = {};
			usersObj.userNumber = userInfo.nowUserName;
			usersObj.userName = userInfo.nowUName;
			usersObj.users = selectUsers[i].userName
			userArr.push(usersObj);
		}
		var str = {
			jsonArrayStr: userArr
		}
		str = JSON.stringify(str);
		console.log(str);
		//保存
		$.ajax({
			url: ajaxUrl + '/user_jurisdiction/insertqx.do',
			type: 'POST',
			data:str,
			contentType : "application/json",
			success: function (data){
				console.log(data);
				//保存成功以后关闭参照页面
				CloseWindow('ok')

			},
			error: function (msg){
				mini.alert('请求失败');
			}
		})
	}else{
		mini.alert('请选择用户');
	}
}
//给父页面传值
function GetData(){
	var selectRows = grid.getSelecteds();
	return selectRows;
}
//关闭子页面
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOk(e) {
    SaveData();
}
function onCancel(e) {
    CloseWindow("cancel");
}
//数据请求
function httpRequest(){
	console.log(userNumber1);
	$.ajax({
        url: ajaxUrl + "/user_jurisdiction/findOne.do",
        type:"post",
		data:{
			userNumber1:userNumber1
		},
        success: function (data) {
        	var userData = data;
        	console.log(userData);
        	// //日期格式化
        	// for(var i = 0; i < userData.length; i++){
        	// 	//创建日期
        	// 	if(userData[i].createDate){
        	// 		userData[i].createDate = formatDate(userData[i].createDate);
        	// 	}else{
        	// 		userData[i].createDate = '-';
        	// 	}
        	// 	//登陆日期
        	// 	if(userData[i].loginDate){
        	// 		userData[i].loginDate = formatDate(userData[i].loginDate);
        	// 	}else{
        	// 		userData[i].loginDate = '-';
        	// 	}
        	// 	//修改日期
        	// 	if(userData[i].updateDate){
        	// 		userData[i].updateDate = formatDate(userData[i].updateDate);
        	// 	}else{
        	// 		userData[i].updateDate = '-';
        	// 	}
        	// 	//用户状态
        	// 	if(userData[i].userState){
        	// 		userData[i].userState = userData[i].userState == 1 ? '账号开启' : '账号关闭';
        	// 	}else{
        	// 		userData[i].userState = '';
        	// 	}
        	// }
        	grid.setData(userData);
        },
        error:function(data){
            mini.alert(data.message);
        }
    });
}